package com.jie.server.core.handler;

import com.jie.common.exception.entity.JieException;
import com.jie.common.http.constant.response.HttpResponseStatusConstant;
import com.jie.common.http.entity.request.HttpRequest;
import com.jie.common.http.entity.response.HttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;

/**
 * @author XieZhiJie
 * @date 2022/04/23 15:21
 */
public class DispatchHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(DispatchHandler.class);

    /**
     * 请求分发并处理
     * @param httpRequest 请求
     * @return            返回响应
     */
    public static HttpResponse dispatchRequestAndHandleRequest(HttpRequest httpRequest) {
        HttpResponse httpResponse;
        try {
            // 执行器, 将请求分发到对应的controller类上, 执行对应的方法
            httpResponse = HttpRequestHandler.handleRequest(httpRequest);
        } catch (NoSuchBeanDefinitionException | NoSuchMethodException | JieException e) {
            LOGGER.warn("请求不存在!");
            httpResponse = new HttpResponse(HttpResponseStatusConstant.NOT_FOUND);
        } catch (Exception e) {
            LOGGER.warn("处理请求出错!");
            httpResponse = new HttpResponse(HttpResponseStatusConstant.INTERNAL_SERVER_ERROR);
        }
        return httpResponse;
    }

}
